var alarms = (function() {
	return {
		"create": (function () {
			return {
				"refresh": function(mins) {
					log.message('Creating Refresh Alarm (' + mins + ')');
					c_alarms.clearAll();
					c_alarms.create(ALARM_REFRESH, {periodInMinutes: mins });
				},
				"gather": function(ms) {
					log.message('Creating Gather Alarm (' + Date.now() + ms + ')');
					c_alarms.clearAll();
					c_alarms.create(ALARM_GATHER, {when: Date.now() + ms});
				},
				"process": function(ms) {
					log.message('Creating Process Alarm (' + Date.now() + ms + ')');
					c_alarms.clearAll();
					c_alarms.create(ALARM_PROCESS, {when: Date.now() + ms});
				},
				"stall": function(ms) {
					c_alarms.clearAll();
					c_alarms.create(ALARM_STALLED, {when: Date.now() + ms});
				}
			}
		}()),
		"clear": function(name) {
			if (name != '') {
				c_alarms.getAll(function(alarmArray) {
					if (alarmArray) {
						if (alarmArray.length > 0) {
							for (x in alarmArray) {
								if (alarmArray[x].name == name) {
									alarms.clear(name);
									break;
								}
							}
						}
					}
				});
			}
		},
		"clearAll": function() {
			c_alarms.clearAll();
		}
	}
}());

var state = {
	"set": function(state) {
		localStorage["process_state"] = state;
	},
	"get": function() {
		return (localStorage["process_state"]) ? Number(localStorage["process_state"]) : state.type.stopped;
	},
	
	"stop": function() {
		localStorage["process_state"] = state.type.stopped;
		alarms.clearAll();
		badge.message('Idle', '#FF0000');
	}
};

var badge = {
	"message": function(message, color) {
		chrome.browserAction.setBadgeText({text: message});
		chrome.browserAction.setBadgeBackgroundColor({color: color});  
	}
}

var log = {
	"message": function(msg) {
		var debug = globalSettings.getSetting("debug_messages");
		
		if (debug) {
			var dt = new Date()
			console.log(dt.toLocaleTimeString() + ' (' + dt.getTime() + ') : ' + msg);
		}
	}
}